layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    var Data = {
        tableId: "contestTable",    //表格id
        condition: {
            name: ""
        }
    };
    /**
     * 初始化表格的列
     */
    Data.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'contestId', sort: true, title: '赛事号', minWidth: 120},
            {field: 'contestName', title: '名称'},
            {field: 'contestDesc', title: '描述'},
            {field: 'registStartTime', sort: true, minWidth: 150, title: '开始时间'},
            {field: 'registEndTime', sort: true, title: '结束时间', minWidth: 150},
            {field: 'remark', title: '备注'},
            {field: 'createdTime', sort: true, title: '创建时间', minWidth: 150},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Data.search = function () {
        var queryData = {};
        queryData['name'] = $("#contestName").val() || Data.condition.name;
        table.reload(Data.tableId, {where: queryData});
    };

    /**
     * 弹出添加
     */
    Data.openAddcontest = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加',
            content: Feng.ctxPath + '/contest/contest_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Data.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Data.exportExcel = function () {
        var checkRows = table.checkStatus(Data.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Data.onEditcontest = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改',
            content: Feng.ctxPath + '/contest/contest_edit?contestId=' + data.contestId,
            end: function () {
                admin.getTempData('formOk') && table.reload(Data.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Data.onDeletecontest = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/contest/delete", function () {
                Feng.success("删除成功!");
                table.reload(Data.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("contestId", data.contestId);
            ajax.start();
        };
        Feng.confirm("是否删除 " + data.contestName + "?", operation);
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Data.tableId,
        url: Feng.ctxPath + '/contest/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Data.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Data.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Data.openAddcontest();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Data.exportExcel();
    });

    Data.createUrl = function (data) {
        var ajax = new $ax(Feng.ctxPath + "/dept/url", function (res) {
            if (res.code == 200) {
                layer.open({
                    type: 0,
                    area: '600px',
                    title:'链接',
                    content: '<p>'+res.data+'</p>'
                });
            } else {
                Feng.error("生成链接失败!");
            }
        }, function (data) {
            Feng.error("生成链接失败!");
        });
        ajax.set("contestId", data.contestId);
        ajax.set("route", "agentregister");
        ajax.start();
    };

    // 工具条点击事件
    table.on('tool(' + Data.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Data.onEditcontest(data);
        } else if (layEvent === 'delete') {
            Data.onDeletecontest(data);
        } else if (layEvent === 'createUrl') {
            Data.createUrl(data);
        }
    });
});
